export enum PlantStage {
    CantPlant,
    WaitPlant,
    SmallPant, //不能再加字段了，不然会影响资源加载，查看花图片加载就明白了
    BigPlant
}

export default class GardenSystem {
    static readonly PotCount = 8;
    static plantSunshineConfig = [2, 5, 10, 15, 20, 30, 50, 100];
    static upgradeCoinConfig = [50, 100, 150, 200, 300, 500, 1000, 2000];
    static produceCoinConfig = [2, 5, 8, 10, 15, 25, 50, 100];
    //花出金币所需的时间间隔(ms)
    static generateCoinInterval = [60000, 150000, 240000, 300000, 450000, 750000, 1500000, 3000000]
    static keyPlantsStage = 'keyPlantsStage'
    static plantsStages = {} as { string: PlantStage }  //0代表灰色，1表示可种，2=小苗 3=成年苗
    static plantsNames = ['Rose', 'chamomile', 'pion', 'hyacinth', 'crocus', 'lavender', 'camellia', 'pansy_blue'];

    static keyFlowerLastCoinTime = 'keyFlowerLastCoinTime'
    static flowersLastCollectCoinTime = {};


    static keyYourSunshine = 'keyYourSunshine';
    static yourSunshine = 0;

    static keyOldSunshineConfig = "keyOldSunshineConfig"
    static load() {
        this.flowersLastCollectCoinTime = JSON.parse(cc.sys.localStorage.getItem(this.keyFlowerLastCoinTime) || "{}") || {}
        this.plantsStages = JSON.parse(cc.sys.localStorage.getItem(this.keyPlantsStage) || "{}") || {}
        this.yourSunshine = parseInt(cc.sys.localStorage.getItem(this.keyYourSunshine) || "0") || 0

        this.makeFirstWaitPot();

        // let gardenVersion = cc.sys.localStorage.getItem(this.keyOldSunshineConfig)
        // if (gardenVersion == null) {
        //     if (GardenSystem.getCurrentWaitFlowerIdx() == 0) {
        //         //还未种过，就用新配置
        //         gardenVersion = "1.1"
        //         cc.sys.localStorage.setItem(this.keyOldSunshineConfig, gardenVersion)
        //     } else {
        //         cc.sys.localStorage.setItem(this.keyOldSunshineConfig, "1.0")
        //     }
        // }
        // if (gardenVersion == "1.1") {
        //     GardenSystem.generateCoinInterval = [60000, 90000, 120000, 150000, 180000, 210000, 240000, 300000]
        // }
    }

    static makeFirstWaitPot() {
        if (this.plantsStages["0"] == null) {
            this.plantsStages['0'] = PlantStage.WaitPlant;
        }
    }

    static getCurrentWaitFlowerIdx() {
        for (let i = 0; i < this.PotCount; ++i) {
            if (this.plantsStages[i.toString()] == PlantStage.WaitPlant)
                return i;
        }
        return -1;
    }

    static getPlantSunshineCnt(idxFrom0: number) {
        return this.plantSunshineConfig[idxFrom0] || 100;
    }

    static getUpgradeCoinCnt(idxFrom0: number) {
        return this.upgradeCoinConfig[idxFrom0] || 1000;
    }

    static getProduceCoin(idxFrom0: number) {
        return this.produceCoinConfig[idxFrom0] || 1;
    }

    static tryToConsumeSunshine(cnt: number) {
        if (this.yourSunshine < cnt)
            return false;
        this.yourSunshine -= cnt;
        cc.sys.localStorage.setItem(this.keyYourSunshine, this.yourSunshine);
        return true;
    }

    /**
     * 种上小苗
     */
    static didPlantFlower(idxFrom0: number) {
        if (idxFrom0 < 0 || idxFrom0 >= this.PotCount)
            return;
        //种上后就开始倒计时
        this.plantsStages[idxFrom0] = PlantStage.SmallPant;
        //解锁下一个待种
        if (idxFrom0 < this.PotCount - 1)
            this.plantsStages[idxFrom0 + 1] = PlantStage.WaitPlant;
        cc.sys.localStorage.setItem(this.keyPlantsStage, JSON.stringify(this.plantsStages));
    }

    static didUpgradePlant(idxFrom0: number) {
        if (idxFrom0 < 0 || idxFrom0 >= this.PotCount)
            return;
        this.plantsStages[idxFrom0] = PlantStage.BigPlant;
        cc.sys.localStorage.setItem(this.keyPlantsStage, JSON.stringify(this.plantsStages));
        //升级完成就可以计时生成金币
        this.flowersLastCollectCoinTime[idxFrom0.toString()] = Date.now();
        cc.sys.localStorage.setItem(this.keyFlowerLastCoinTime, JSON.stringify(this.flowersLastCollectCoinTime));
    }

    //已收金币
    static didCollectCoin(idxFrom0: number) {
        if (idxFrom0 < 0 || idxFrom0 >= this.PotCount)
            return;
        this.flowersLastCollectCoinTime[idxFrom0.toString()] = Date.now();
        cc.sys.localStorage.setItem(this.keyFlowerLastCoinTime, JSON.stringify(this.flowersLastCollectCoinTime));
    }

    static getPlantState(idxFrom0: number): PlantStage {
        if (idxFrom0 < 0 || idxFrom0 >= this.PotCount)
            return PlantStage.CantPlant;
        return this.plantsStages[idxFrom0.toString()] || PlantStage.CantPlant;
    }

    static getPlantName(idxFrom0: number) {
        return this.plantsNames[idxFrom0]
    }

    static getLeftTimeInMillSecond(idxFrom0: number) {
        // console.log(`花${idxFrom0} 上次:${(new Date(this.flowersLastCollectCoinTime[idxFrom0]).toLocaleTimeString())}`)
        return this.generateCoinInterval[idxFrom0] - (Date.now() - this.flowersLastCollectCoinTime[idxFrom0]);
    }

    //
    static addSunshine(cnt: number) {
        this.yourSunshine += cnt;
        cc.sys.localStorage.setItem(this.keyYourSunshine, this.yourSunshine);
    }
}